package com.fbw.sys.uitls;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class WebUtils {
    public static ServletRequestAttributes getServletRequestAttributes(){

        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        /*
        RequestContextHolder
        RequestContextHolder顾名思义,持有上下文的Request容器.使用是很简单的，它所有方法都是static的
该类主要维护了两个全局容器()
         */
    }

    //获取当前线程的请求对象
    public static HttpServletRequest getHttpServletRequest() {
        return getServletRequestAttributes().getRequest();
    }

    //获取当前线程的响应对象
    public static HttpServletResponse getHttpServletResponse() {
        return getServletRequestAttributes().getResponse();
    }

    public static HttpSession getHttpSession() {
        return getHttpServletRequest().getSession();
    }

    public static ServletContext getServletContext() {
        WebApplicationContext WebApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        ServletContext servletContext = WebApplicationContext.getServletContext();
        return servletContext;
        //return getHttpServletRequest().getServletContext();
        //通过
        /*
        不太懂，等会试试能不能用。

        可用

        作为工具类获取ServletContext
         */
    }

}
